package com.czlis.boe;

import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import com.alibaba.csb.sdk.HttpCaller;
import com.alibaba.fastjson.JSON;
public class MesProductionClient {

	
	public static void main(String args[]){
		Map<String,String> map1 = new HashMap<String, String>();
		map1.put("startDate", "2018-12-19");
		map1.put("endDate", "2018-12-19");
		map1.put("startTime","08:00:00");
		map1.put("endTime", "20:00:00");
		map1.put("shipper", "B8");
		map1.put("oemMill", "1003653");
		map1.put("proModel", "BH156A");
		map1.put("fgcode", "NT156WHM-N42-8D36");
		map1.put("cellNo", "");
		map1.put("orderNo", "B8P156WH5TP02-P");
		
		map1.put("siteStep", "1008");
		map1.put("waitIn", "100");
		map1.put("tackIn", "100");
		map1.put("wipHold", "100");
		map1.put("wipRelease", "100");
		map1.put("tackOut", "100");
		map1.put("makeLoss", "100");
		map1.put("cellLoss", "100");
		map1.put("rawLoss", "100");
		map1.put("reWaitAgain", "100");
		map1.put("rewordkInAgain", "100");
		map1.put("rewordkOutAgain", "100");
		map1.put("rewordkIn", "100");
		map1.put("rewordkWip", "100");
		map1.put("rewordkOut", "100");
		map1.put("workstop", "1");
		map1.put("proLine", "1");
		map1.put("incommingModel", "1");
		
		
		List<Map<String,String>> list = new ArrayList<Map<String,String>>();
		
		list.add(map1);
		
		Map<String,Object> map2 = new HashMap<String, Object>();
//		map2.put("INTERFACEID", "1");
	//	map2.put("TRANSID", "1");
		//map2.put("MESSAGEID", "1");
//		map2.put("SENDER", "1");
	//	map2.put("RECEIVER", "1");
		map2.put("item", list);
		
		Map<String,String> map3 = new HashMap<String, String>();
		map3.put("mesProductions", JSON.toJSONString(map2));
		
		//Map<String,Object> map3 = new HashMap<String, Object>();
		//map3.put("mesProductions", map2);
		//Map<String,String> map4 = new HashMap<String, String>();
		//map4.put("addProductionData", JSON.toJSONString(map3));
		
		System.out.println(JSON.toJSON(map3));
		//测试外网
		String requestURL = "https://210.12.224.195:443/test";
		//String requestURL = "https://10.80.25.151:8086/test";
		String API_NAME = "MESPRODUCTIONDATA";
		String ak = "82f16f644ac34f6e968764322f4bd7b0";
		String sk = "jgAgaxG8/O9zmc+yx626aJDZsW8=";
		//生产外网
		/*String requestURL = "https://api-csb-broker.boe.com.cn:443/test";
		String API_NAME = "MESPRODUCTIONDATA";
		String ak ="257a453ef52e4498979a2c317c79622f";
		String sk ="bp9zFdmDI18mBKbYlM8bDgPCvO4=";*/
		
		try {
//			  String result = doGet();
			String result = HttpCaller.doPost(requestURL,API_NAME, "1.0.0", map3, ak, sk);
//			String result = doPost(ak, sk, requestURL+"/1.0.0/"+API_NAME, JSON.toJSONString(map2));
//			  String result = doGet(requestURL, API_NAME, "1.0.0" ,map1, ak, sk);
			  if (result != null) {
				 System.out.println("******************************");
				 String result1 = new String(result.getBytes("GB2312"), "ISO-8859-1");
//				 String result2 = new String(result.getBytes("UTF-8"),"ISO-8859-1");
//				 String result3 = new String(result.getBytes("ISO8859-1"),"ISO-8859-1");
//				 String result4 = new String(result.getBytes("GBK"),"ISO-8859-1");
				 System.out.println(result1); 
		
			  }
			} catch (Exception ie) {
				ie.printStackTrace();
			}
	}
	
	/**
     * 绕过验证
     *     
     * @return
     * @throws NoSuchAlgorithmException 
     * @throws KeyManagementException 
     */
    public static SSLContext createIgnoreVerifySSL() throws NoSuchAlgorithmException, KeyManagementException {
        SSLContext sc = SSLContext.getInstance("SSLv3");

        // 实现一个X509TrustManager接口，用于绕过验证，不用修改里面的方法
        X509TrustManager trustManager = new X509TrustManager() {
            @Override
            public void checkClientTrusted(
                    java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
                    String paramString) throws CertificateException {
            }

            @Override
            public void checkServerTrusted(
                    java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
                    String paramString) throws CertificateException {
            }

            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };

        sc.init(null, new TrustManager[] { trustManager }, null);
        return sc;
    }
    
    
	
	
	public static String doPost(String ak, String sk, String url, String data) {
		String result = "";
		HttpPost post = new HttpPost(url);// 创建httpClient的post请求对象
		// 设置请求参数
		HttpParams params = new BasicHttpParams();
		HttpConnectionParams.setConnectionTimeout(params, 5 * 1000);
		HttpConnectionParams.setSoTimeout(params, 5 * 1000);
		// 传值
		// 创建List集合，存放向服务器传递的数据
		List<NameValuePair> reqData = new ArrayList<NameValuePair>();
		NameValuePair p1 = new BasicNameValuePair("ak", ak);
		NameValuePair p2 = new BasicNameValuePair("sk", sk);
		NameValuePair p3 = new BasicNameValuePair("mesProductions", data);
		reqData.add(p1);
		reqData.add(p2);
		reqData.add(p3);

		try {
			HttpEntity entity = new UrlEncodedFormEntity(reqData, "utf-8");
			post.setEntity(entity);// 给post请求对象，设置上传的数据
			HttpClient client = new DefaultHttpClient(params);// 创建HttpClient对象
			HttpResponse res = client.execute(post);// 执行post请求，获得结果
			if (res.getStatusLine().getStatusCode() == 200) {
				HttpEntity resEntity = res.getEntity();// 获得相应结果，是一个HttpEntity对象
				result = EntityUtils.toString(resEntity, "utf-8");// 转换为字符串
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
}
